package cn.yuan.user.controller;

import cn.yuan.basic.util.PageList;
import cn.yuan.user.domain.Logininfo;
import cn.yuan.user.query.LogininfoQuery;
import cn.yuan.user.service.ILogininfoService;
import cn.yuan.util.AjaxResult.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
@Api(tags = "登录接口",description = "登录接口详细描述")
public class LogininfoController {
    @Autowired
    private ILogininfoService logininfoService;

    @GetMapping("/{id}")
    @ApiOperation(value = "登录查找",notes = "根据ID查找登录")
    public Logininfo findOne(@PathVariable("id")Long id){
        return logininfoService.loadById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有登录",notes = "根据ID查找登录")
    public List<Logininfo> findAll(){
        return logininfoService.loadAll();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除登录",notes = "根据ID删除登录")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            logininfoService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除登录",notes = "根据ID删除登录")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        System.out.println(ids);
        try {
            logininfoService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改登录",notes = "添加或者修改登录")
    public AjaxResult addOrUpdate(@RequestBody Logininfo logininfo){
        try {
            if(logininfo.getId()==null){
                logininfoService.save(logininfo);
            }else {
                logininfoService.update(logininfo);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "查询分页",notes = "分页查询登录")
    public PageList<Logininfo> queryForPage(@RequestBody LogininfoQuery logininfoQuery){
        return logininfoService.queryForPage(logininfoQuery);
    }
}
